const $pagination = require("./pagination")

module.exports = async ({ list = [], threads = 10, fn }) => {
    let result = []
    let paginationList = $pagination({ list, pageSize: threads })

    for (let i = 0, l = paginationList.length; i < l; i++) {
        let { list } = paginationList[i]
        let allSettled = []
        list.forEach((item, j) => {
            let index = (i * threads) + j
            allSettled.push(fn(item, index))
        })
        let array = await Promise.allSettled(allSettled)
        result.push(...array.map(item => item.value))
    }
    return result
}